package com.software_product_construction.backend_software_product_construction.service.impl;

import com.software_product_construction.backend_software_product_construction.dao.UserMapper;
import com.software_product_construction.backend_software_product_construction.dao.example.UserExample;
import com.software_product_construction.backend_software_product_construction.pojo.User;
import com.software_product_construction.backend_software_product_construction.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> findAll(String account) {
        UserExample example = new UserExample();
        example.createCriteria().andAccountLike(account);
        return userMapper.selectByExample(example);
    }

    @Override
    public String update(User user) {

        String account = user.getAccount();
        UserExample example = new UserExample();
        example.createCriteria().andAccountEqualTo(account);
        example.setOrderByClause("history");
        List<User> list =  userMapper.selectByExample(example);
        User user0 = list.get(list.size());
        user.setHistory(user0.getHistory()+1);
        int result = userMapper.insertSelective(user0);
        if(result ==1){
            return "success";
        }
        return "false";
    }

    @Override
    public List<User> getHistory(String userId, int from, int to) {
         to = to -from;
         return userMapper.getHistory(userId,from,to);
    }

    @Override
    public String update1(String account) {
        UserExample example = new UserExample();
        example.createCriteria().andAccountEqualTo(account);
        example.setOrderByClause("history");
        List<User> list =  userMapper.selectByExample(example);
        System.out.println(list);
        int result = 1;
        if(result ==1){
            return "success";
        }
        return "false";
    }
}
